using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using usm.SolDev.DataBase.DataObjects;

namespace usm.SolDev.UI
{
    public partial class FilesBrowser : ToolWindow
    {
        #region Private fields
        private const string constFileExtention = "*.sde";
        #endregion

        #region Open project delegate
        public delegate void OpenProjectDelegate(object sender, ProjectDescriptor projectDescriptor);
        public event OpenProjectDelegate OpenProject;
        #endregion

        #region Constructors
        public FilesBrowser()
        {
            InitializeComponent();
        }
        #endregion

        #region Helper events
        /// <summary>
        ///     Load available files on the specified path 
        /// </summary>
        /// <param name="filesPath"></param>
        /// <param name="filesFilter"></param>
        private void LoadFiles(string filesPath, string filesFilter)
        {
            lvFiles.Items.Clear();

            if (filesPath != null)
            {
                try
                {
                    foreach (string file in System.IO.Directory.GetFiles(filesPath, filesFilter))
                    {
                        ListViewItem item = new ListViewItem( System.IO.Path.GetFileNameWithoutExtension(file));

                        #region load project
                        try
                        {
                            item.Tag = (new ProjectDescriptor()).LoadFromFile(file);
                            item.SubItems.Add(((ProjectDescriptor)item.Tag).Description);
                            item.SubItems.Add(((ProjectDescriptor)item.Tag).LastEditTime.ToString());
                        }
                        catch { }
                        #endregion

                        lvFiles.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    new SolDev.Common.SolDevException("Cannot complete files loading", ex);
                }
            }
        }
        #endregion

        #region Form events handler

        /// <summary>
        ///     Perform initial data loading
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileBrowser_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        ///     On folder changed event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileBrowser_SelectedFolderChanged(object sender, FileBrowser.SelectedFolderChangedEventArgs e)
        {
            if (e.Item.IsFileSystem && e.Item.IsFolder)
            {
                LoadFiles(e.Path, constFileExtention);
            }
            else
            {
                LoadFiles(null, constFileExtention);
            }

        }

        /// <summary>
        ///     The handler for file change event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvFiles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            propertyGrid1.SelectedObject = (ProjectDescriptor)e.Item.Tag;
        }

        #endregion
    }
}

